<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2019/8/13
 * Time: 19:07
 */

namespace app\common\behavior;


use think\Db;

class ActionLog
{

    public function run(&$params)
    {

        if (request()->module() != 'admin'){
            return false;
        }

        $url = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', request()->controller())).'/'.strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', request()->action()));

        //不批量操作日志的路由
        $noRecord = [
            'rule/detail',
            'index/index',
            'user/change_money',
            'money/edit'
        ];

        if (!in_array($url,$noRecord)){

            $session = session('user_id');

            $title = Db::name('auth_rule')->where('name',$url)->cache(15)->value('title');

            if (!$title){
                $title1 = explode('/',$url);
                $map['control'] = $title1[0];
                $map['act']         = $title1[1];
                $title = Db::name('right')->where($map)->cache(15)->value('name');
            }

            $log['params']      = json_encode(input('param.'));
            $log['user_id']     = $session??0;
            $log['url']         = request()->url();
            $log['title']       = $title??'';
            $log['create_time'] = time();
            $log['update_time'] = time();
            $log['ip']          = request()->ip();
            Db::name('action_log')->insert($log);
        }

    }
}